package com.itbour.service.tg.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itbour.com.page.JspPage;
import com.itbour.com.page.Page;
import com.itbour.common.util.StringUtils;
import com.itbour.dao.tg.MappingDao;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.tg.MapPCInfoDto;
import com.itbour.dto.tg.MapPageInfoDto;
import com.itbour.dto.tg.MappingInfoDto;
import com.itbour.dto.tg.OutlineInfoDto;
import com.itbour.dto.tg.SizeInfoDto;
import com.itbour.service.tg.MappingService;
import com.opensymphony.xwork2.ActionContext;


/**
* 分页映射
* 
* copyright   itbour team 2016
* @author     xc
*
*/

public class MappingServiceImpl implements MappingService {

	private MappingDao mappingDao;
	
	/**
	 * @param countKey
	 *            查询该表所有记录行的SQL语句ID
	 * @param sqlKey
	 *            需要查询记录的SQL语句ID
	 * @param param
	 *            传递给查询语句的参数
	 * @param pageNo
	 *            需要查询的实际页
	 * @param pageSize
	 *            每页记录行数
	 * @return page对象
	 */
	public Page pageQuery(Map<String,Object> param, Integer pageNo, Integer pageSize) {
		try {
			Page result = null;
			
			// 实际起始位置
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = mappingDao.selectMappingCnt(param);
			
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			
			// 分页检索
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<MappingInfoDto> data = mappingDao.selectMappingInfo(param);

			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
			//clearSelectDataSource(dataSource);
		}
	}
	
	// 查询分页映射
	public List<MappingInfoDto> selectMappingInfo(Map<String,Object> param) {
		return mappingDao.selectMappingInfo(param);
	}
	public List<MapPageInfoDto> selectMapPageInfo(Map<String,Object> param) {
		return mappingDao.selectMapPageInfo(param);
	}
	public List<MapPCInfoDto> selectMapPCInfo(Map<String,Object> param) {
		return mappingDao.selectMapPCInfo(param);
	}

	// 更新分页映射信息
	public void updateMappingById(Map<String,Object> param) {
		mappingDao.updateMappingInfoById(param);
	}
	
	// 删除分页映射
	public void deleteMappingInfo(Map<String,Object> param) {
		mappingDao.deleteMappingLogic(param);
	}
	public void deleteMapPageInfo(Map<String,Object> param) {
		mappingDao.deleteMapPageLogic(param);
	}
	public void deleteMapPCInfo(Map<String,Object> param) {
		mappingDao.deleteMapPCLogic(param);
	}
	
	// 新规分页映射doc
	public Long addMappingInfo(MappingInfoDto info) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("tg_id", info.getTg_id());
		param.put("map_name", info.getMap_name());
		param.put("user_id", info.getUser_id());
		param.put("user_type", info.getUser_type());
		param.put("publish", info.getPublish());
		param.put("data", info.getData());
		param.put("hash_tag", info.getHash_tag());
		param.put("size_id", info.getSize_id());
		param.put("type_id", info.getType_id());
		param.put("ver", info.getVer());
		param.put("update_by", info.getUpdate_by());
		mappingDao.insertMappingInfo(param);
		
		if(param.containsKey("map_id")){
			return (Long)param.get("map_id");
		}
		return null;
	}
	// 新规分页映射page
	public Long addMapPageInfo(MapPageInfoDto info) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("map_id", info.getMap_id());
		param.put("page_index", info.getPage_index());
		param.put("backup_flg", info.getBackup_flg());
		param.put("data", info.getData());
		param.put("hash_tag", info.getHash_tag());
		param.put("thumb", info.getThumb());
		param.put("update_by", info.getUpdate_by());
		mappingDao.insertMapPageInfo(param);
		
		if(param.containsKey("page_id")){
			return (Long)param.get("page_id");
		}
		return null;
	}
	// 新规分页映射pc
	public Long addMapPCInfo(MapPCInfoDto info) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("page_id", info.getPage_id());
		param.put("map_id", info.getMap_id());
		param.put("pc_index", info.getPc_index());
		param.put("backup_flg", info.getBackup_flg());
		param.put("data", info.getData());
		param.put("replaces", info.getReplaces());
		param.put("hash_tag", info.getHash_tag());
		param.put("thumb_s", info.getThumb_s());
		param.put("dirty", info.getDirty());
		param.put("update_by", info.getUpdate_by());
		mappingDao.insertMapPCInfo(param);
		
		if(param.containsKey("pc_id")){
			return (Long)param.get("pc_id");
		}
		return null;
	}
	
	// 查询提纲
	public List<OutlineInfoDto> selectOutlineInfo(Map<String,Object> param) {
		return mappingDao.selectOutlineInfo(param);
	}

	// 尺寸信息
	public List<SizeInfoDto> selectSizeInfoList(Map<String,Object> param) {
		return mappingDao.selectSizeInfo(param);
	}
		
	public MappingDao getMappingDao() {
		return mappingDao;
	}

	public void setMappingDao(MappingDao mappingDao) {
		this.mappingDao = mappingDao;
	}

}
